<%@page import="models.User"%>
<%@include file="../template/configuration.jsp" %>
<%@page import="models.Course"%>
<%@page import="java.util.List"%>
<%@page import="classes.HibernateUtil"%>
<%@page import="dao.CourseDao"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
if (request.getParameter("category_id") != null && request.getParameter("page") != null) {
    HibernateUtil hu = new HibernateUtil();
    CourseDao cd = new CourseDao(hu.openSession());
    List<Course> courses = cd.getPagination(Long.parseLong(request.getParameter("category_id")), Integer.parseInt(request.getParameter("page")), 4);
    pageContext.setAttribute("courses", courses);
    
    HttpSession ses = request.getSession();

    if(ses.getAttribute("user") != null) {
        User user = (User) ses.getAttribute("user");
        if (ses.getAttribute("userrole").equals("Student")) {
            List<Course> ownedCourses = cd.findAllByStudent(user.getMember_id());
            pageContext.setAttribute("ownedCourses", ownedCourses);
        }
    }
    
}
%>
<c:forEach items="${courses}" var="course">
    <fieldset class="inside-block">
        <legend align="center">
            <div class="button no-link"><a>${course.name}</a></div>
        </legend>
        <div class="left">
            <img src="${url}images/course-default.png" />
        </div>
        <div class="right">
            <h4>${uq_description}</h4>
            ${course.description}
        </div>
        <div class="bottom">
            <div class="button float-right"><a href="">${uq_view_course}</a></div>
            <c:set var="owned" value="false" />
            <c:forEach items="${ownedCourses}" var="mycourse">
                <c:if test="${mycourse.id == course.id}">
                    <c:set var="owned" value="true" />
                </c:if>
            </c:forEach>
            <c:if test="${owned == 'false'}">
                <c:choose>
                    <c:when test="${sessionScope.cart==null}">
                        <div class="button float-right"><a href="CartController?courseid=${course.id}" class="add">${uq_add_to_cart}</a></div>                                                                
                    </c:when>
                    <c:otherwise>
                        <c:set var="incart" value="false"/>
                        <c:forEach items="${sessionScope.cart.cart}" var="cart">
                            <c:if test="${cart.id==course.id}"><c:set var="incart" value="true"/></c:if>
                        </c:forEach>
                            <c:if test="${incart==true}">
                        <div class="button float-right"><a href="CartController?courseid=${course.id}" class="remove-from-cart">${uq_remove_from_cart}</a></div>    
                        </c:if>
                        <c:if test="${incart==false}">
                        <div class="button float-right"><a href="CartController?courseid=${course.id}" class="add">${uq_add_to_cart}</a></div>    
                        </c:if>                                                                    
                        <c:set var="incart" value="false"/>
                    </c:otherwise>
                </c:choose>                                                                                                                
                <div class="button free-lesson float-right"><a href="">${uq_free_lesson}</a></div>
            </c:if>
        </div>
    </fieldset>
    <div class="clear margin-bottom-20"></div>
</c:forEach>